package com.example.wallpaperapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.example.wallpaperapp.R;

import java.util.ArrayList;

public  class StaggeredGridAdapter extends RecyclerView.Adapter<StaggeredGridAdapter.ViewHolder> {

    private ArrayList<Integer> lists = new ArrayList<>();

    public void replaceAll(ArrayList<Integer> list) {
        lists.clear();
        if (list != null && list.size() > 0) {
            lists.addAll(list);
        }
        notifyDataSetChanged();
    }
    public void addData(int position,ArrayList<Integer> list) {
        lists.addAll(position,list);
        notifyItemInserted(position);
    }

//    移除数据使用notifyItemRemoved
    public void removeData(int position) {
        lists.remove(position);
        notifyItemRemoved(position);
    }



    @Override
    public StaggeredGridAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OneViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
    }

    public void onBindViewHolder(@NonNull StaggeredGridAdapter.ViewHolder holder, int position) {
        holder.setData(lists.get(position),position);
    }


    @Override
    public int getItemCount() {
        return lists != null ? lists.size() : 0;
    }


    public class ViewHolder  extends RecyclerView.ViewHolder{

        public ViewHolder(View itemView) {
            super(itemView);
        }
        void setData(Object data,int position) {
        }
    }
    private class OneViewHolder extends ViewHolder {
            private ImageView ivImage;
            public OneViewHolder(View view) {
                super(view);
                ivImage =  view.findViewById(R.id.iv);
            }
        void setData(Object data,int position) {
            if (data != null) {
                int id = (int) data;
                ivImage.setImageResource(id);
                ivImage.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 550));
            }
        }
    }

}

